package com.ruanda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruanda.constant.StoreConstants;
import com.ruanda.domain.Notice;
import com.ruanda.mapper.NoticeMapper;
import com.ruanda.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * @author 亦樂
 * @description 针对表【notice】的数据库操作Service实现
 * @createDate 2024-09-06 14:26:11
 */
@Service
@CacheConfig(cacheNames = "com.ruanda.service.impl.NoticeServiceImpl")
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice>
        implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    @Override
    @Caching(evict = {
            @CacheEvict(StoreConstants.WX_TOP_NOTICE),
            @CacheEvict(StoreConstants.WX_ALL_NOTICE)
    })
    public Boolean saveNotice(Notice notice) {
        notice.setShopId(1L);
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());
        return noticeMapper.insert(notice) > 0;
    }

    @Override
    @Caching(evict = {
            @CacheEvict(StoreConstants.WX_TOP_NOTICE),
            @CacheEvict(StoreConstants.WX_ALL_NOTICE)
    })
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @Override
    @Caching(evict = {
            @CacheEvict(StoreConstants.WX_TOP_NOTICE),
            @CacheEvict(StoreConstants.WX_ALL_NOTICE)
    })
    public Boolean modifyNotice(Notice notice) {
        notice.setUpdateTime(new Date());
        return noticeMapper.updateById(notice) > 0;
    }

    @Override
    @Cacheable(StoreConstants.WX_TOP_NOTICE)
    public List<Notice> queryWxTopNoticeList() {
        return noticeMapper.selectList(new LambdaQueryWrapper<Notice>()
                .eq(Notice::getStatus, 1)
                .eq(Notice::getIsTop, 1)
                .orderByDesc(Notice::getCreateTime)
        );
    }

    @Override
    @Cacheable(StoreConstants.WX_ALL_NOTICE)
    public List<Notice> queryWxAllNoticeList() {
        return noticeMapper.selectList(new LambdaQueryWrapper<Notice>()
                .eq(Notice::getStatus, 1)
                .orderByDesc(Notice::getCreateTime)
        );
    }
}




